package com.hxd.tabactivityfragment.mvp.presenter

import android.content.Intent
import android.support.v4.app.FragmentActivity
import android.view.View
import com.hxd.tabactivityfragment.bean.IMPItem
import com.hxd.tabactivityfragment.mvp.base.IBasePresenter
import com.hxd.tabactivityfragment.mvp.model.HomeModel
import com.hxd.tabactivityfragment.mvp.view.IHomeView
import com.hxd.tabactivityfragment.ui.activity.WebActivity
import com.hxd.tabactivityfragment.util.BannerDataUtil
import com.hxd.tabactivityfragment.util.ThreadUtil
import com.xuexiang.xui.widget.banner.anim.select.ZoomInEnter
import com.xuexiang.xui.widget.banner.transform.ZoomOutSlideTransformer
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem
import com.xuexiang.xui.widget.banner.widget.banner.SimpleImageBanner



/**
 *  项目名：    FoodOrigins
 *  包名：      com.hxd.tabactivityfragment.mvp.presenter
 *  文件名:     HomePresenter
 *  创建者:     HXD
 *  创建时间:   2019/4/1 21:35
 *  描述:
 */

class HomePresenter(val activity: FragmentActivity) :IBasePresenter{

    private val mHomeModel by lazy { HomeModel() }
    private var mHomeView:IHomeView? = null
    val REQUEST_CODE_ZXing:Int = 100

    /**
     * 初始化item数据
     */
    fun initItemData(){
        mHomeModel.getItemData(object : HomeModel.OnHomeItemListener{
            override fun getHomeItemSuccess(impItem: IMPItem) {
                mHomeView!!.getItemDataSuccess(impItem.msg.rows)
            }

            override fun getHomeFailure(msg: String) {
                mHomeView!!.getDataFail(msg)
            }
        })
    }

    /**
     * 加载更多item数据
     */
    fun loadItemData(size:Int){
        mHomeModel.loadItemData(size,object : HomeModel.OnHomeItemListener{
            override fun getHomeItemSuccess(impItem: IMPItem) {
                mHomeView!!.getItemDataSuccess(impItem.msg.rows)
            }

            override fun getHomeFailure(msg: String) {
                mHomeView!!.getDataFail(msg)
            }
        } )
    }




    /**
     * 初始化轮播图
     */
    fun initBeannerData(view:View){
        mHomeModel.getBannerData(object :HomeModel.OnHomeBannerListener{
            override fun getBannerDataSuccess(bannerData: ArrayList<BannerItem>) {
                ThreadUtil.runOnMainThread(Runnable {
                    //配置轮播图
                    (view as SimpleImageBanner).setSelectAnimClass(ZoomInEnter::class.java)
                        .setSource(BannerDataUtil.getDatas())
                        .setTransformerClass(ZoomOutSlideTransformer::class.java)
                        .setOnItemClickL{
                            var intent = Intent(activity, WebActivity::class.java)
                            intent.putExtra("url", BannerDataUtil.clickArray[it])
                            activity.startActivity(intent)
                        }.startScroll()
                })
                mHomeView!!.getBannerDataSuccess(bannerData)
            }

            override fun getHomeFailure(msg: String) {
                mHomeView!!.getDataFail(msg)
            }
        })
    }

    /**
     * 解绑
     */
    override fun destoryView() {
        if (mHomeView != null){
            mHomeView = null
        }
    }

    /**
     * 绑定
     */
    override fun bindView(view: Any) {
        mHomeView = view as IHomeView
    }
}